/**
 * Copyright (C) 2009 Noel Ang
 *
 * This file is part of Samsara.
 *
 * Samsara is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Samsara is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Samsara.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package com.lunarshades.samsara.ui.actions;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.net.URL;
import java.text.MessageFormat;
import java.util.Arrays;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.lunarshades.samsara.Raider;
import com.lunarshades.samsara.ui.RosterTableModel;
import com.lunarshades.samsara.ui.SamsaraFrame;
import static com.lunarshades.samsara.ui.actions.RosterChangeEvent.RosterChangeType;

/**
 * Action responsible for removing entries in a RosterTableModel.
 *
 * @author Noel Ang, sparta.variable@gmail.com
 * @see com.lunarshades.samsara.ui.RosterTableModel
 */
public class RosterRemoveAction<E extends Raider> extends RosterChangeAction
        implements ListSelectionListener {

public RosterRemoveAction(RosterTableModel<E> model,
                          JTable table,
                          SamsaraFrame app
) {
    super(NAME);
    mTable = table;
    mModel = model;
    mApp = app;

    URL smallIconUrl = getClass().getResource(
            "/com/lunarshades/samsara/ui/icons/delete16.gif"
    );
    putValue(Action.SMALL_ICON, new ImageIcon(smallIconUrl));
    putValue(Action.SHORT_DESCRIPTION, SHORT_TEXT);
    putValue(Action.MNEMONIC_KEY, KeyEvent.VK_D);
    setEnabled(!table.getSelectionModel().isSelectionEmpty());
    // Listen to selection changes to enable/disable the action as appropriate
    table.getSelectionModel().addListSelectionListener(this);
}

public void actionPerformed(ActionEvent e) {
    int[] selectedRows = mTable.getSelectedRows();

    if (selectedRows.length > 0) {
        for (int i = 0; i < selectedRows.length; ++i) {
            selectedRows[i] = mTable.convertRowIndexToModel(selectedRows[i]);
        }
        Arrays.sort(selectedRows);
    }
    synchronized (mModel) {
        for (int i = selectedRows.length - 1; i >= 0; --i) {
            try {
                E entry = mModel.getRowAt(selectedRows[i]);
                mModel.setRowAt(null, selectedRows[i]);
                mApp.postStatusMessage(MessageFormat.format(REMOVE_STATUS,
                        entry.name()
                )
                );
                fireRosterChanged(entry, RosterChangeType.REMOVE);
            } catch (Exception e1) {
                assert false;
            }
        }
    }
}

public void valueChanged(ListSelectionEvent e) {
    // Nothing to remove if nothing is selected
    setEnabled(!mTable.getSelectionModel().isSelectionEmpty());
}

private static final String NAME = "Delete Raider";
private static final String SHORT_TEXT = "Remove the selected table entry";
private static final String REMOVE_STATUS = "Removed: ''{0}''.";

private final JTable mTable;
private final RosterTableModel<E> mModel;
private final SamsaraFrame mApp;
}
